package com.imot.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author ning.wang
 * @since 2023/5/25
 */
public class JwtUtil {
    public static final long EXPIRE = 1000 * 60 * 60 * 24; //token的过期时间
    public static final String APP_SECRET="ukc8BDbRigUDaY6pZFfWus2jZWLPHO";//密钥

    //生成Token字符串的方法
    public static String getJwtToken(String id,String nickname){
        String JwtToken= Jwts.builder()
                .setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS256")

                .setSubject("imot-tech")
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))

                .claim("id", id)  //设置Token主体部分，存储用户信息
                .claim("nickname",nickname)

                .signWith(SignatureAlgorithm.HS256,APP_SECRET) //设置Token的密钥
                .compact();
        return JwtToken;
    }

    //判断Token是否有效
    public static boolean checkToken(String jwtToken){
        if(StringUtils.isEmpty(jwtToken)) return false;
        try {
            Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
    //判断Token是否存在与有效
    public static boolean checkToken(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            if(StringUtils.isEmpty(token)) return false;
            Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //根据Token获取id
    public static String getNumberIdByJwtToken(HttpServletRequest request){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)) return "";
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        return (String) body.get("id");
    }
    //根据Token获取姓名
    public static String getNumbernicknameByJwtToken(HttpServletRequest request){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)) return "";
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        return (String) body.get("nickname");
    }
    //根据Token获取id
    public static String getNumberIdByJwtToken(String token){
        if(StringUtils.isEmpty(token)) return "";
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        return (String) body.get("id");
    }
    //根据Token获取用户名
    public static String getNicknameByJwtToken(String token){
        if(StringUtils.isEmpty(token)) return "";
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        return (String) body.get("nickname");
    }
}
